//
//  SPSetProgramInfo.m
//  Sampux
//
//  Created by rzm on 16/8/24.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SPSetProgramInfo.h"

@implementation SPSetProgramInfo

+ (NSInteger)setWeekDayWithBinaryToDecimalSystem:(NSString *)binary
{
    int ll = 0 ;
    int  temp = 0 ;
    for (int i = 0; i < binary.length; i ++)
    {
        temp = [[binary substringWithRange:NSMakeRange(i, 1)] intValue];
        temp = temp * powf(2, binary.length - i - 1);
        ll += temp;
    }
    
    NSInteger result = ll;
    return result;
}

//  十进制转二进制
+ (NSString *)toBinarySystemWithDecimalSystem:(NSString *)decimal
{
    int num = [decimal intValue];
    int remainder = 0;      //余数
    int divisor = 0;        //除数
    
    NSString * prepare = @"";
    
    while (true)
    {
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%d",remainder];
        
        if (divisor == 0)
        {
            break;
        }
    }
    
    NSString * result = @"";
    for (int i = prepare.length - 1; i >= 0; i --)
    {
        result = [result stringByAppendingFormat:@"%@",
                  [prepare substringWithRange:NSMakeRange(i , 1)]];
    }
    
    return result;
}


+ (int)Thirty_SixBinarySystemToDecimalSystem:(NSString *)numStr
{
    
    const char * str = [numStr UTF8String];
    int t = 36;
    int len = strlen(str);
    int result=0;
    char cur;
    int i=0;
    for(;i<len;i++)
    {
        cur=*(str+i);
        if(cur>='0'&&cur<='9'&&(cur-'0')<t)//处理0-9的字符，不能包含大于等于进制数的字符
        {
            result*=t;
            result+=cur-'0';
            continue;
        }
        if(cur>='A'&&cur<='Z'&&(cur-'A')<t-10)//处理A-Z的字符，不能包含大于等于进制数的字符
        {
            result*=t;
            result+=cur-'A'+10;
        }else return -1;
    }
    return result;
}
@end
